package com.example.test.service;

import com.example.test.entity.*;
import com.example.test.mapper.UserMapper;
import com.example.test.mapper.UserRoleMapper;
import com.example.test.mapper.RoleMapper;
import com.example.test.mapper.NewsMapper;
import com.example.test.mapper.NewsCommentsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserService {

    //@Resource来自jdk中的注解，可以使用它给引用类型赋值注入(这就是spring的自动注入使用)
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private NewsCommentsMapper newsCommentsMapper;

    public List<User> findAll() {
        return userMapper.findAll();
    }

    public List<News> findAllNews() {
        return newsMapper.findAll();
    }

    public List<News> findNewComments() {
        return newsMapper.findNewComments();
    }


    public User findByUserName(String userName){
        return userMapper.findByUserName(userName);
    }

    public List<Map<String,Object>> findNewComments2(Integer offset,Integer pageSize) {
        return newsMapper.findNewComments2(offset,pageSize);
    }

    public List<User> userList() {
        return userMapper.findAll();
    }

    public List<User> userList2() {
        List<Integer> userIds = new ArrayList<>();
        List<Integer> roleIds = new ArrayList<>();
        Map<Integer, User> userList = new HashMap<>();
        //获取用户列表
        List<User> user = userMapper.findAll();
        for (int i = 0; i < user.size(); i++) {
            userList.put(user.get(i).getId(), user.get(i));
            userIds.add(user.get(i).getId());
        }
        System.out.println(userIds);
        //获取用户角色列表
        List<UserRole> userRole = userRoleMapper.findAllByUid(userIds);
        for (int k = 0; k < userRole.size(); k++) {
            roleIds.add(userRole.get(k).getRoleId());
        }

        Map<Integer, String> userRoleName = new HashMap<>();
        //获取角色列表
        List<Role> role = roleMapper.findAllByRoleId(roleIds);
        for (int h = 0; h < role.size(); h++) {
            int key = role.get(h).getId();
            for (int k = 0; k < userRole.size(); k++) {
                if (userRole.get(k).getRoleId() == key) {
                    userRoleName.put(userRole.get(k).getUid(), role.get(h).getName());
                }
            }
        }
        System.out.println(userRoleName);

        Set<Integer> keySet = userRoleName.keySet();
        for (Integer key : keySet){
            if(userList.containsKey(key)){
                String roleName = userRoleName.get(key);
                User u = userList.get(key);
                u.setRoleName(roleName);
                userList.put(key,u);
            }
        }
        List<User> list = new ArrayList<>(userList.values());
        return list;
    }

    public List<User> selectPage(int pageNum, int pageSize) {
        return userMapper.selectPage(pageNum, pageSize);
    }

    public int save(User user) {
        return userMapper.save(user);
    }

    public Integer delete(int id) {
        return userMapper.delete(id);
    }

    public User get(int id) {
        return userMapper.get(id);
    }

    public int update(User user) {
        return userMapper.update(user);
    }

    public int saveUserRole(UserRole userRole) {
        return userRoleMapper.save(userRole);
    }


}